﻿using System;
using System.ComponentModel;
using System.IO;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;


namespace YoutubeVideoManager
{
    /// <summary>
    /// Logica di interazione per VlcControl.xaml
    /// </summary>
    public partial class VlcControl : UserControl
    {
        #region Properties
        #endregion

        #region Fields
        Point leftDown;
        DispatcherTimer clock;
        #endregion

        //#region Methods
        public VlcControl()
        {
            InitializeComponent();
            clock = new DispatcherTimer();
            clock.Interval = TimeSpan.FromMilliseconds(500);
            clock.Tick += new EventHandler(clock_Tick);
            if (!DesignerProperties.GetIsInDesignMode(this))
                clock.Start();
        }

        void clock_Tick(object sender, EventArgs e)
        {
            // aggiorno le label
            txtPos.Text = TimeSpan.FromSeconds(VLC.Position).ToString();
            txtLength.Text = TimeSpan.FromSeconds(VLC.Length).ToString();

            // aggiorno la progress bar
            prgBar.Maximum = VLC.Length;
            prgBar.Value = VLC.Position;
        }

        private void cmdPlay_Click(object sender, RoutedEventArgs e)
        {
            VLC.PlayPause();
        }
        private void cmdNext_Click(object sender, RoutedEventArgs e)
        {
            VLC.Next();
        }

        private void cmdPrev_Click(object sender, RoutedEventArgs e)
        {
            VLC.Prev();
        }

        private void prgBar_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            leftDown = e.GetPosition(prgBar);
        }

        private void prgBar_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Point currentUp = e.GetPosition(prgBar);
            Vector diff = leftDown - currentUp;
            if (Math.Abs(diff.X) <= 5 && Math.Abs(diff.Y) <= 5)
            {
                double prg = currentUp.X / prgBar.RenderSize.Width * prgBar.Maximum;
                VLC.Seek((int)prg);
            }
        }

    }
}
